package com.xyh.concurrency.example.concueerncyAqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class CyclicBarrierDemo1 {
    private static CyclicBarrier cyclicBarrier = new CyclicBarrier(20, () -> {
        //这种写法就是等线程所有的一段线程执行完后就会执行当前这段类中方法
        log.info("执行完成");
    });

    public static void main(String[] args) {

        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            final int threadNum = i;
            //创建十条线程池
            executorService.execute(() -> {
                try {
                    rede(threadNum);
                } catch (Exception e) {

                }
            });
        }
    }

    public static void rede(int threadNum) throws Exception {
        Thread.sleep(1000);
        log.info("{} res", threadNum);
        cyclicBarrier.await();
        log.info("jieshu");
    }
}
